package com.wdl.sm.webui.security;

import java.util.ArrayList;
import java.util.Collection;

import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;

public class SecurityUser extends SUser implements UserDetails {
    private static final long serialVersionUID = 6921820883499619091L;

    public SecurityUser(SUser user) {
        if (user != null) {
            this.setId(user.getId());
            this.setName(user.getName());
            this.setDesc(user.getDesc());
            this.setTel(user.getTel());
            this.setPassword(user.getPassword());
            this.setRole(user.getRole());
        }
    }

    @Override
    public Collection<? extends GrantedAuthority> getAuthorities() {
        Collection<GrantedAuthority> authorities = new ArrayList<GrantedAuthority>();
        String role = super.getRole();
        if (role == null || role.isEmpty()) {
            role = ROLE_VIEW;
        }
        
        authorities.add(new SimpleGrantedAuthority(role));

        return authorities;
    }

    @Override
    public String getUsername() {
        return super.getName();
    }

    @Override
    public String getPassword() {
        return super.getPassword();
    }

    @Override
    public boolean isAccountNonExpired() {
        return true;
    }

    @Override
    public boolean isAccountNonLocked() {
        return true;
    }

    @Override
    public boolean isCredentialsNonExpired() {
        return true;
    }

    @Override
    public boolean isEnabled() {
        return true;
    }
}
